import requests

# 配置你的 GitLab 服务器 URL 和 Personal Access Token
GITLAB_URL = "https://git.realthon.com"
PRIVATE_TOKEN = "glpat-wxyZPwNvPTvfrdfxxA4d"

headers = {
    "PRIVATE-TOKEN": PRIVATE_TOKEN,
    "Content-Type": "application/json"
}

# 获取 group 的 ID
def get_group_id(group_path):
    url=f"{GITLAB_URL}/api/v4/groups?search={group_path}"
    print(url)
    response = requests.get(f"{GITLAB_URL}/api/v4/groups?search={group_path}", headers=headers)
    if response.status_code == 200:
        groups = response.json()
        for group in groups:
            print(f"Response for group search ({group['full_path']}): {group_path}")
            if group['full_path'] == group_path.lower():
                return group['id']
    return None

# 子组和项目命名
groups_projects = {
    "hobot-system-ext/auto_sw/J3-MO-PlatformSDK": ["app", "appuser", "build", "kernel", "kernel-5.15", "manifest", "toolchain", "uboot", "vendor"],
    "hobot-system-ext/auto_sw/J3-MO-PlatformSDK/hbre": ["camera", "diagnose", "hbutils", "libalsa", "libhapi", "libhbaudio", "libspi", "libvio", "selinux", "sensor", "middleware", "third_party", "viotool"],
    "hobot-system-ext/auto_sw/J3-MO-PlatformSDK/prebuilts": ["host", "ko", "root", "root_hijack", "usr"],
    "hobot-system-ext/auto_sw/J3-MO-PlatformSDK/unittest": ["prebuilts", "testapp", "utest"],
    "mcu_dev/gd_dev/gd32g5xx": ["ADC"]
}

def create_project(group_path, project_name):
    group_id = get_group_id(group_path)
    if group_id is None:
        print(f"Group {group_path} not found.")
        return
    data = {
        "name": project_name,
        "namespace_id": group_id
    }
    response = requests.post(f"{GITLAB_URL}/api/v4/projects", headers=headers, json=data)
    if response.status_code == 201:
        print(f"Created project: {project_name} in group: {group_path}")
    else:
        print(f"Failed to create project: {project_name} in group: {group_path}, Response: {response.text}")

for group_path, projects in groups_projects.items():
    for project in projects:
        # create_project(group_path, project)
        group_id = get_group_id(group_path)
        print(group_path, group_id, project)
